﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Cryptography;
using Analysis.Entities;
using Dapper;
using System.Data;
using System.Data.SqlClient;

namespace Analysis.DAL.Repositories
{
    public class SaleRepository : BaseDataRepository
    {
        public static DataTable FindSale()
        {
            string strSQL = @"select s.*, m.MonthName 
                            from Sale (nolock) as s left join Months (nolock) as m on s.MonthId = m.MonthId 
                            where IsDelete = 0";
            return DataAccess.GetDataTable(strSQL);
        }

        public static void EditSale(int id, List<KeyValuePair<string, object>> data)
        {
            string strSQL;
            List<string> parameters = new List<string>();
            List<object> values = new List<object>();

            if (id == -1)
            {
                strSQL = "insert into Sale ";
                strSQL += GenerateInsertDynamic(data, parameters, values);
            }
            else
            {
                strSQL = "update Sale set ";
                strSQL += GenerateUpdateDynamic(data, parameters, values);
                strSQL += " where SaleId = " + id.ToString();
            }
            DataAccess.ExecuteSQL(strSQL, parameters.ToArray(), values.ToArray()); 
        }

        public static void DeleteSale(string id)
        {
            DataAccess.ExecuteProcedure("spSale_Delete", new string[] { "@SaleId" }, new object[] { id });
        }

        public static DataTable YearCombo()
        {
            int year = DateTime.Today.Year;
            string strSQL = string.Format(@"select distinct YearId from Sale (nolock)
                            union select {0} union select {1} union select {2}
                            order by 1", (year - 1).ToString(), year.ToString(), (year + 1).ToString());
            return DataAccess.GetDataTable(strSQL);
        }
    }
}
